﻿using AccumulateApp.Model;
using CommunityToolkit.Mvvm.ComponentModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace AccumulateApp.View
{
    /// <summary>
    /// Interaction logic for EnumListToValueTest.xaml
    /// </summary>
    public partial class EnumListToValueTest : UserControl
    {
        public EnumListToValueTest()
        {
            InitializeComponent();

            this.DataContext = new EnumListToValueTestViewModel();
        }
    }

    public class EnumListToValueTestViewModel : ObservableObject
    {
        private int _selectedHobbies;

        public int SelectedHobbies
        {
            get => _selectedHobbies;
            set
            {
                _selectedHobbies = value;
                OnPropertyChanged(nameof(SelectedHobbies));
                UpdateHobbyCopies();
            }
        }

        public List<HobbyModel> Hobbies { get; set; }
        public List<HobbyModel> HobbyCopies { get; set; }

        public EnumListToValueTestViewModel()
        {
            Hobbies = new List<HobbyModel>
            {
                new HobbyModel(Hobby.Swimming, UpdateHobbySelections),
                new HobbyModel(Hobby.Sport, UpdateHobbySelections),
                new HobbyModel(Hobby.Reading, UpdateHobbySelections),
                new HobbyModel(Hobby.Music, UpdateHobbySelections),
                new HobbyModel(Hobby.Travel, UpdateHobbySelections),
                new HobbyModel(Hobby.Movie, UpdateHobbySelections),
                new HobbyModel(Hobby.Game, UpdateHobbySelections)
            };

            HobbyCopies = new List<HobbyModel>
            {
                new HobbyModel(Hobby.Swimming),
                new HobbyModel(Hobby.Sport),
                new HobbyModel(Hobby.Reading),
                new HobbyModel(Hobby.Music),
                new HobbyModel(Hobby.Travel),
                new HobbyModel(Hobby.Movie),
                new HobbyModel(Hobby.Game)
            };

        }

        private void UpdateHobbySelections(HobbyModel hobby)
        {
            // SelectedHobbies is a bitmask representing selected hobbies
            if (hobby.IsSelected)
            {
                SelectedHobbies |= (int)hobby.Value; // Set the bit
            }
            else
            {
                SelectedHobbies &= ~(int)hobby.Value; // Clear the bit
            }
        }

        private void UpdateHobbyCopies()
        {
            foreach (var hobby in HobbyCopies)
            {
                hobby.IsSelected = (SelectedHobbies & (int)hobby.Value) != 0;
            }
        }
    }

    public class HobbyModel : ObservableObject
    {
        private bool _isSelected;
        public bool IsSelected
        {
            get { return _isSelected; }
            set
            {
                // 检测界面更新方法一
                _isSelected = value;
                OnPropertyChanged(nameof(IsSelected));
                OnSelectedChanged?.Invoke(this);
            }
        }

        public string Name { get; set; }
        public Hobby Value { get; set; }
        public Action<HobbyModel> OnSelectedChanged { get; set; }

        public HobbyModel(Hobby hobby)
        {
            IsSelected = false;
            Name = hobby.ToString();
            Value = hobby;
        }

        public HobbyModel(Hobby hobby, int selections)
        {
            Name = hobby.ToString();
            Value = hobby;
        }

        public HobbyModel(Hobby hobby, Action<HobbyModel> updateAction)
        {
            IsSelected = false;
            Name = hobby.ToString();
            Value = hobby;
            OnSelectedChanged = updateAction;
        }
    }

    [Flags]
    public enum Hobby
    {
        Swimming = 1,
        Sport = 2,
        Reading = 4,
        Music = 8,
        Travel = 16,
        Movie = 32,
        Game = 64
    }
}
